package com.lvmama.rhino.web;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.lvmama.rhino.base.BaseControllerSupport;
import com.lvmama.rhino.entity.BarDataVo;
import com.lvmama.rhino.entity.ChildConditionVO;
import com.lvmama.rhino.entity.FilterConditionVO;
import com.lvmama.rhino.entity.LineDataVo;
import com.lvmama.rhino.entity.ProductInfoVO;
import com.lvmama.rhino.entity.RequestSearchParam;
import com.lvmama.rhino.entity.SearchConditionVO;
import com.lvmama.rhino.entity.SecondConditionVO;
import com.lvmama.rhino.enums.CommonParentType;
import com.lvmama.rhino.enums.CommonSearchFilter;
import com.lvmama.rhino.enums.CommonSearchType;
import com.lvmama.rhino.enums.CommonStringType;
import com.lvmama.rhino.pojo.search.FlowSearchArrivalKey;
import com.lvmama.rhino.pojo.search.FlowSearchFilter;
import com.lvmama.rhino.pojo.search.FlowSearchFilterCondition;
import com.lvmama.rhino.pojo.search.FlowSearchJump;
import com.lvmama.rhino.pojo.search.FlowSearchKey;
import com.lvmama.rhino.pojo.search.FlowSearchKeyType;
import com.lvmama.rhino.pojo.search.FlowSearchPaging;
import com.lvmama.rhino.pojo.search.FlowSearchResult;
import com.lvmama.rhino.pojo.search.FlowSearchStation;
import com.lvmama.rhino.pojo.search.FlowSearchStatistics;
import com.lvmama.rhino.service.FlowSearchService;
import com.lvmama.rhino.service.StationService;
import com.lvmama.rhino.service.StatisticCodeService;
import com.lvmama.rhino.utils.DateUtil;
import com.lvmama.rhino.utils.SearchSectionUtil;

/**
 * @ClassName: SearchStatisticsController
 * @Description:搜索相关请求
 * @author shenlinli
 * @date 2017年3月13日 下午5:36:38
 */
@Controller
@RequestMapping("searchStatistic")
public class SearchStatisticsController extends BaseControllerSupport {
	@Autowired
	private FlowSearchService flowSearchService;
	@Autowired
	private StationService service;
	@Autowired
	private StatisticCodeService statisticCodeService;

	/**
	 * @Title: searchFlux
	 * @Description: TODO 搜索流量
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	@RequestMapping("searchFlux")
	public String searchFlux() {
		return "application/searchStatistic/searchFlux";
	}

	/**
	 * @Title: searchAjaxData
	 * @Description: 根据查询条件获取数据
	 * @param @param choose_date 时间段
	 * @param @param platform_code 平台
	 * @param @return 设定文件
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	@RequestMapping("searchAjaxData")
	@ResponseBody
	public Map<String, Object> searchAjaxData(String choose_date,
			String platform_code) {

		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(choose_date);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");

		// 获取平台相关code
		List<String> statisticCodeList = getPlatformCodeList(platform_code);
		RequestSearchParam param = new RequestSearchParam();
		param.setBeginDate(beginDate);
		param.setEndDate(endDate);
		param.setStatisticCodeList(statisticCodeList);

		List<FlowSearchStatistics> searchByChannelList = flowSearchService
				.getSearchFlowByChannel(param);
		List<FlowSearchStatistics> searchByDatelList = flowSearchService
				.getSearchFlowByDate(param);
		/* 柱状图数据封装 */
		Map<String, Long> mapForBar = new HashMap<String, Long>();
		for (FlowSearchStatistics flowSearchStatistics : searchByChannelList) {
			String channelCode = getChannelName(flowSearchStatistics
					.getChannelCode());
			if (mapForBar.containsKey(channelCode)) {
				long amount = mapForBar.get(channelCode)
						+ flowSearchStatistics.getAmount();
				mapForBar.put(channelCode, amount);
			} else {
				mapForBar.put(channelCode, flowSearchStatistics.getAmount());
			}
		}
		JSONArray barData = parseJsonForBar(mapForBar);

		/* 折线图数据封装 */
		Map<String, Long> mapForLine = new HashMap<String, Long>();
		for (FlowSearchStatistics flowSearchStatistics : searchByDatelList) {
			String channelCode = getChannelName(flowSearchStatistics
					.getChannelCode());
			String date = new SimpleDateFormat("yyyy/MM/dd")
					.format(flowSearchStatistics.getOperDate());
			String key = channelCode + "_" + date;
			long amount = 0;
			if (mapForLine.containsKey(key)) {
				amount = flowSearchStatistics.getAmount() + mapForLine.get(key);
			} else {
				amount = flowSearchStatistics.getAmount();
			}
			mapForLine.put(channelCode + "_" + date, amount);
		}
		JSONArray lineData = parseJsonForLine(mapForLine);

		Map<String, Object> resultMap = new java.util.HashMap<>();
		resultMap.put("lineData", lineData);
		resultMap.put("barData", barData);
		return resultMap;
	}

	@RequestMapping("export")
    public void exportData(String choose_date,String platform_code,String type,HttpServletResponse response,HttpServletRequest request) throws IOException{
		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(choose_date);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");

		// 获取平台相关code
		List<String> statisticCodeList = getPlatformCodeList(platform_code);
		RequestSearchParam param = new RequestSearchParam();
		param.setBeginDate(beginDate);
		param.setEndDate(endDate);
		param.setStatisticCodeList(statisticCodeList);
		
		
		
		// 生成提示信息
		response.setContentType("application/octet-stream");
		BufferedWriter bd=new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), "gbk"),1024);
		if(type.equals("2")){
			//查询柱状图数据
			List<FlowSearchStatistics> searchByChannelList = flowSearchService
					.getSearchFlowByChannel(param);
			
			// 进行转码，使其支持中文文件名
			String fileName = java.net.URLEncoder.encode("流量总量统计", "UTF-8");
			response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
			
			
			String Header = "\"渠道\""+","+"\"流量\"";
			bd.write(Header);
			 
			//柱状图下载
			for (FlowSearchStatistics fss : searchByChannelList) {  
				bd.newLine();
				List<String> csvLine=new ArrayList<String>();
				csvLine.add(getChannelName(fss.getChannelCode()));
				csvLine.add(fss.getAmount().toString());
				StringBuffer sb = new StringBuffer();
				for (int i = 0; i < csvLine.size(); i++) {  
		            String line = csvLine.get(i);  
		            if (line == null) {  
		                line = "";  
		            }  
		            sb.append("\"").append(line.replaceAll("\"", "\"\"")).append("\",");  
		        }
				bd.write(sb.deleteCharAt(sb.length() - 1).toString());  
			 }	
		}else{
			//查询折线图数据
			List<FlowSearchStatistics> searchByDatelList = flowSearchService
					.getSearchFlowByDate(param);
			
			// 进行转码，使其支持中文文件名
			String fileName = java.net.URLEncoder.encode("流量趋势分析", "UTF-8");
			response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
			
			String Header = "\"日期\""+","+"\"渠道\""+","+"\"流量\"";
			bd.write(Header);
			 
			//柱状图下载
			for (FlowSearchStatistics fss : searchByDatelList) {  
				bd.newLine();
				List<String> csvLine=new ArrayList<String>();
				DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
				csvLine.add(df.format(fss.getOperDate()).toString());
				csvLine.add(getChannelName(fss.getChannelCode()));
				csvLine.add(fss.getAmount().toString());
				
				StringBuffer sb = new StringBuffer();
				for (int i = 0; i < csvLine.size(); i++) {  
		            String line = csvLine.get(i);  
		            if (line == null) {  
		                line = "";  
		            }  
		            sb.append("\"").append(line.replaceAll("\"", "\"\"")).append("\",");  
		        }
				bd.write(sb.deleteCharAt(sb.length() - 1).toString());  
			 }	
			
		}
		
       
		bd.flush();
		bd.close();
	         
	}
	
	/**
	 * @Title: getChannelName
	 * @Description: TODO 根据code 区分渠道
	 * @param @param channelCode
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	private static String getChannelName(String channelCode) {
		if (channelCode.startsWith("12h")) {// SEM code码以12h开头
			return "SEM";
		} else if (channelCode.startsWith("123")) {// SEO code码以123开头
			return "SEO";
		} else if (channelCode.equals("116yq5")) {// 自有渠道 同义 直接流量
			return "直接流量";
		} else {
			return "其他";
		}
	}

	/**
	 * @Title: keyword
	 * @Description: 关键字分析
	 * @param @param modelMap
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	@RequestMapping("keyword")
	public String keyword(ModelMap modelMap) {
		List<FlowSearchStation> stationList = flowSearchService.getStations();
		modelMap.addAttribute("stationList", stationList);
		return "application/searchStatistic/keyword";
	}

	/**
	 * @Title: keyAjaxData
	 * @Description: 搜索关键词分析获取折线图 柱状图数据
	 * @param @param choose_date 时间段
	 * @param @param platform_code 平台
	 * @param @param station_id 站点
	 * @param @return 设定文件
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	@RequestMapping("keyAjaxData")
	@ResponseBody
	public Map<String, Object> keyAjaxData(String choose_date,
			String platform_code, int station_id) {
		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(choose_date);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");

		// 获取平台相关code
		List<String> statisticCodeList = getPlatformCodeList(platform_code);
		RequestSearchParam param = new RequestSearchParam();
		param.setBeginDate(beginDate);
		param.setEndDate(endDate);
		param.setStatisticCodeList(statisticCodeList);
		param.setStation_id(station_id);

		List<FlowSearchKeyType> serachListByKey = flowSearchService
				.getKeyByType(param);
		List<FlowSearchKeyType> serachListByDate = flowSearchService
				.getKeyByDate(param);

		/* 柱状图数据封装 */
		List<BarDataVo> dataList = new ArrayList<BarDataVo>();
		for (FlowSearchKeyType flowSearchKey : serachListByKey) {
			String type = CommonStringType.getKeyNameByValue(flowSearchKey
					.getKeyType());
			if (StringUtils.isNotEmpty(type)) {
				BarDataVo vo = new BarDataVo();
				vo.setAmount(flowSearchKey.getAmount());
				vo.setCategory(type);
				dataList.add(vo);
			}
		}
		JSONArray barData = JSONArray.fromObject(dataList);

		/* 折线图数据封装 */
		Map<String, Long> mapForLine = new HashMap<String, Long>();
		for (FlowSearchKeyType flowSearchKey : serachListByDate) {
			String type = CommonStringType.getKeyNameByValue(flowSearchKey
					.getKeyType());
			String date = new SimpleDateFormat("yyyy/MM/dd")
					.format(flowSearchKey.getOperDate());
			String key = type + "_" + date;
			long amount = 0;
			if (mapForLine.containsKey(key)) {
				amount = flowSearchKey.getAmount() + mapForLine.get(key);
			} else {
				amount = flowSearchKey.getAmount();
			}
			mapForLine.put(key, amount);
		}
		JSONArray lineData = parseJsonForLine(mapForLine);

		Map<String, Object> resultMap = new java.util.HashMap<>();
		resultMap.put("lineData", lineData);
		resultMap.put("barData", barData);
		return resultMap;
	}

	/**
	 * @Title: keyWordAjaxData
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param choose_date
	 * @param @param stationForKey
	 * @param @param searchSetion
	 * @param @return 设定文件
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	@RequestMapping("keyWordAjaxData")
	@ResponseBody
	public Map<String, Object> keyWordAjaxData(String choose_date,
			int stationForKey, String sectionIds, int currentPage,
			int page_size, int flag) {
		List<SearchConditionVO> conditionList = new ArrayList<SearchConditionVO>();
		if (org.apache.commons.lang.StringUtils.isNotEmpty(sectionIds)) {
			for (String section_id : sectionIds.substring(0,
					sectionIds.length() - 1).split(",")) {
				conditionList.add(SearchSectionUtil.getSectionCondition(Integer
						.parseInt(section_id)));
			}
		}
		Map<String, Object> resultMap = new java.util.HashMap<String, Object>();

		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(choose_date);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");

		RequestSearchParam param = new RequestSearchParam();
		param.setBeginDate(beginDate);
		param.setEndDate(endDate);
		param.setStation_id(stationForKey);
		param.setConditionList(conditionList);
		param.setPage_currentNum(currentPage);
		param.setPage_size(page_size);
		param.setFlag(flag);

		PageInfo<FlowSearchKey> page = flowSearchService.getKeyByAmount(param);
		resultMap.put("page", page);
		return resultMap;
	}

	/**
	 * @Title: typeAStationAjaxData
	 * @Description: 根据所在地分组
	 * @param @param choose_date
	 * @param @param stationForKey
	 * @param @param sectionIds
	 * @param @param key_word
	 * @param @return 设定文件
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	@RequestMapping("typeAStationAjaxData")
	@ResponseBody
	public Map<String, Object> typeAStationAjaxData(String choose_date,
			int stationForKey, String sectionIds, String key_word) {
		List<SearchConditionVO> conditionList = new ArrayList<SearchConditionVO>();
		if (org.apache.commons.lang.StringUtils.isNotEmpty(sectionIds)) {
			for (String section_id : sectionIds.substring(0,
					sectionIds.length() - 1).split(",")) {
				conditionList.add(SearchSectionUtil.getSectionCondition(Integer
						.parseInt(section_id)));
			}
		}
		Map<String, Object> resultMap = new java.util.HashMap<String, Object>();

		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(choose_date);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");

		RequestSearchParam param = new RequestSearchParam();
		param.setBeginDate(beginDate);
		param.setEndDate(endDate);
		param.setStation_id(stationForKey);
		param.setConditionList(conditionList);
		param.setKey_word(key_word);

		List<FlowSearchKey> serachForStations = flowSearchService
				.getDetailByStation(param);
		List<FlowSearchKey> serachForType = flowSearchService
				.getDetailByType(param);

		Map<String, Integer> keyTypeMap = new HashMap<String, Integer>();
		if (serachForType.size() > 0) {
			for (FlowSearchKey flowSearchKey : serachForType) {
				String key_type_map = flowSearchKey.getKeyTypeMap();
				if (StringUtils.isNotEmpty(key_type_map)) {
					JSONObject object = JSONObject.fromObject(key_type_map);
					for (Iterator<?> iter = object.keys(); iter.hasNext();) {
						String key = (String) iter.next();
						int value = Integer.parseInt(object.getString(key));
						int totalValue = value;
						if (keyTypeMap.containsKey(key)) {
							totalValue = keyTypeMap.get(key) + value;
						}
						keyTypeMap.put(key, totalValue);
					}
				}
			}
		}

		/* type数据封装 */
		List<BarDataVo> dataList = new ArrayList<BarDataVo>();
		for (Entry<String, Integer> entry : keyTypeMap.entrySet()) {
			String type = CommonStringType.getKeyNameByValue(entry.getKey());
			if (StringUtils.isNotEmpty(type)) {
				BarDataVo vo = new BarDataVo();
				vo.setAmount(entry.getValue());
				vo.setCategory(type);
				dataList.add(vo);
			}
		}
		JSONArray keyData = JSONArray.fromObject(dataList);

		Map<String, Integer> stationMap = new HashMap<String, Integer>();
		if (serachForStations.size() > 0) {
			for (FlowSearchKey flowSearchKey : serachForStations) {
				String prov_map = flowSearchKey.getProvMap();
				if (StringUtils.isNotEmpty(prov_map)) {
					JSONObject object = JSONObject.fromObject(prov_map);
					for (Iterator<?> iter = object.keys(); iter.hasNext();) {
						String key = (String) iter.next();
						int value = Integer.parseInt(object.getString(key));
						int totalValue = value;
						if (stationMap.containsKey(key)) {
							totalValue = stationMap.get(key) + value;
						}
						stationMap.put(key, totalValue);
					}
				}
			}
		}
		stationMap = sortByValueDesc(stationMap);
		/* station数据封装 */
		String flag = "false";
		int i = 0;
		List<BarDataVo> stationList = new ArrayList<BarDataVo>();
		//List<BarDataVo> stationAllList = new ArrayList<BarDataVo>();
		for (Entry<String, Integer> entry : stationMap.entrySet()) {
			BarDataVo vo = new BarDataVo();
			vo.setAmount(entry.getValue());
			vo.setCategory(entry.getKey());
			if (i < 10) {
				stationList.add(vo);
				i++;
			}else{
				break;
			}
			/*if (stationMap.size() > 10) {
				flag = "true";
				stationAllList.add(vo);
			}*/
		}
		JSONArray stationData = JSONArray.fromObject(stationList);
		/*JSONArray stationAllData = JSONArray.fromObject(stationAllList);*/

		resultMap.put("keyData", keyData);
		resultMap.put("stationData", stationData);
		/*resultMap.put("stationAllData", stationAllData);*/
		resultMap.put("flag", flag);
		return resultMap;
	}

	/**
	 * @Title: sortByValueDesc
	 * @Description: map降序排序
	 * @param @param map
	 * @param @return 设定文件
	 * @return Map<K,V> 返回类型
	 * @throws
	 */
	private static <K, V extends Comparable<? super V>> Map<K, V> sortByValueDesc(
			Map<K, V> map) {
		List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(
				map.entrySet());
		Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
			public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
				return (o2.getValue()).compareTo(o1.getValue());
			}
		});
		Map<K, V> result = new LinkedHashMap<K, V>();
		for (Map.Entry<K, V> entry : list) {
			result.put(entry.getKey(), entry.getValue());
		}
		return result;
	}

	/**
	 * @Title: sortByValueAsc
	 * @Description: map升序排序
	 * @param @param map
	 * @param @return 设定文件
	 * @return Map<K,V> 返回类型
	 * @throws
	 */
	private static <K, V extends Comparable<? super V>> Map<K, V> sortByValueAsc(
			Map<K, V> map) {
		List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(
				map.entrySet());
		Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
			public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
				return (o1.getValue()).compareTo(o2.getValue());
			}
		});

		Map<K, V> result = new LinkedHashMap<K, V>();
		for (Map.Entry<K, V> entry : list) {
			result.put(entry.getKey(), entry.getValue());
		}
		return result;
	}

	@RequestMapping("keyForCategoryAjaxData")
	@ResponseBody
	public Map<String, Object> keyForCategoryAjaxData(String choose_date,
			String platform_code, int stationForCategory) {

		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(choose_date);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");

		// 获取平台相关code
		List<String> statisticCodeList = getPlatformCodeList(platform_code);
		RequestSearchParam param = new RequestSearchParam();
		param.setBeginDate(beginDate);
		param.setEndDate(endDate);
		param.setStatisticCodeList(statisticCodeList);
		param.setStation_id(stationForCategory);

		List<FlowSearchArrivalKey> searchByCategoryList = flowSearchService
				.getKeyByCategory(param);

		List<BarDataVo> dataList = new ArrayList<BarDataVo>();
		for (FlowSearchArrivalKey flowSearchKey : searchByCategoryList) {
			String name = CommonParentType.getCategoryShowName(flowSearchKey
					.getCategoryId());
			if (StringUtils.isNotEmpty(name)) {
				BarDataVo vo = new BarDataVo();
				vo.setAmount(flowSearchKey.getSearchKeyAmount());
				vo.setCategory(name);
				dataList.add(vo);
			}
		}
		JSONArray keyCategoryData = JSONArray.fromObject(dataList);
		Map<String, Object> resultMap = new java.util.HashMap<String, Object>();
		resultMap.put("keyCategoryData", keyCategoryData);
		return resultMap;
	}

	@RequestMapping("keyForKeyWordAjaxData")
	@ResponseBody
	public Map<String, Object> keyForKeyWordAjaxData(String choose_date,
			String platform_code, int stationForCategory, String category_name) {

		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(choose_date);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");

		// 获取平台相关code
		List<String> statisticCodeList = getPlatformCodeList(platform_code);
		RequestSearchParam param = new RequestSearchParam();
		param.setBeginDate(beginDate);
		param.setEndDate(endDate);
		param.setStatisticCodeList(statisticCodeList);
		param.setStation_id(stationForCategory);
		param.setCategory_id(CommonParentType
				.getCategoryValueByShowName(category_name));

		List<FlowSearchArrivalKey> searchByWordList = flowSearchService
				.getKeyByKeyword(param);

		Map<String, Integer> keyMap = new HashMap<String, Integer>();
		for (FlowSearchArrivalKey flowSearchKey : searchByWordList) {
			keyMap.put(flowSearchKey.getSearchKey(),
					flowSearchKey.getSearchKeyAmount());
		}
		int i = 0;
		keyMap = sortByValueDesc(keyMap);

		List<BarDataVo> dataList = new ArrayList<BarDataVo>();
		for (Entry<String, Integer> entry : keyMap.entrySet()) {
			if (i < 10) {
				BarDataVo vo = new BarDataVo();
				vo.setAmount(entry.getValue());
				vo.setCategory(entry.getKey());
				dataList.add(vo);
				i++;
			} else {
				break;
			}

		}
		JSONArray keyWordData = JSONArray.fromObject(dataList);

		Map<String, Object> resultMap = new java.util.HashMap<String, Object>();
		resultMap.put("keyWordData", keyWordData);
		return resultMap;
	}

	@RequestMapping("keyForXgWordAjaxData")
	@ResponseBody
	public Map<String, Object> keyForXgWordAjaxData(String choose_date,
			String platform_code, Integer stationForCategory,
			String category_name, String key_word, int flag, int currentPage,
			int page_size) {

		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(choose_date);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");

		// 获取平台相关code
		List<String> statisticCodeList = getPlatformCodeList(platform_code);
		RequestSearchParam param = new RequestSearchParam();
		param.setBeginDate(beginDate);
		param.setEndDate(endDate);
		param.setStatisticCodeList(statisticCodeList);
		param.setStation_id(stationForCategory);
		param.setCategory_id(CommonParentType
				.getCategoryValueByShowName(category_name));
		param.setKey_word(key_word);
		List<FlowSearchArrivalKey> keyXgwordList = flowSearchService
				.getKeyByXgword(param);
		List<ProductInfoVO> productVoList = new ArrayList<ProductInfoVO>();
		Map<String, Integer> productMap = new HashMap<String, Integer>();
		for (FlowSearchArrivalKey flowSearchArrivalKey : keyXgwordList) {
			String map = flowSearchArrivalKey.getSearchMap();
			JSONObject jsonObject = JSONObject.fromObject(map);
			for (Iterator<?> iter = jsonObject.keys(); iter.hasNext();) {
				String key = (String) iter.next();
				int value = Integer.parseInt(jsonObject.getString(key));
				if (productMap.containsKey(key)) {
					value += productMap.get(key);
				}
				productMap.put(key, value);
			}
		}
		if (flag == 1) {
			productMap = sortByValueAsc(productMap);
		} else {
			productMap = sortByValueDesc(productMap);
		}

		for (Map.Entry<String, Integer> entry : productMap.entrySet()) {
			ProductInfoVO vo = new ProductInfoVO();
			String[] keyArr = new String[2];
			keyArr[0] = entry.getKey().split(",")[0];
			if (entry.getKey().split(",").length > 1) {
				keyArr[1] = entry.getKey().split(",")[1];
			}
			vo.setProduct_id(keyArr[0]);
			vo.setProduct_name(keyArr[1] == null ? "" : keyArr[1]);
			vo.setProduct_num(entry.getValue());
			productVoList.add(vo);
		}
		Map<String, Object> resultMap = new java.util.HashMap<String, Object>();
		PageInfo<ProductInfoVO> page = getListByPage(productVoList, page_size,
				currentPage);
		resultMap.put("page", page);
		return resultMap;
	}

	@RequestMapping("skipAnalysis")
	public String skipAnalysis(ModelMap modelMap) {
		List<FlowSearchStation> stationList = flowSearchService.getStations();
		modelMap.addAttribute("stationList", stationList);
		return "application/searchStatistic/skipAnalysis";
	}

	@RequestMapping("jumpAjaxData")
	@ResponseBody
	public Map<String, Object> jumpAjaxData(String flow_type,
			String choose_date, String platform_code, int station_id,
			String key_type) {

		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(choose_date);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");

		// 获取平台相关code
		List<String> statisticCodeList = getPlatformCodeList(platform_code);
		RequestSearchParam param = new RequestSearchParam();
		param.setBeginDate(beginDate);
		param.setEndDate(endDate);
		param.setStatisticCodeList(statisticCodeList);
		param.setStation_id(station_id);
		if ("pv".equals(flow_type)) {
			param.setJumpTableName("flow_search_jump_pv");
		} else {
			param.setJumpTableName("flow_search_jump_uv");
		}
		List<FlowSearchJump> searchByCategoryList = new ArrayList<>();
		List<FlowSearchJump> searchByDatelList = new ArrayList<>();
		if ("切换标签".equals(key_type)) {
			searchByCategoryList = flowSearchService.getSwitchByCategory(param);
			searchByDatelList = flowSearchService.getSwitchByDate(param);
		} else {
			searchByCategoryList = flowSearchService.getJumpByCategory(param);
			searchByDatelList = flowSearchService.getJumpByDate(param);
		}
		/* 柱状图数据封装 */
		List<BarDataVo> dataList = new ArrayList<BarDataVo>();
		for (FlowSearchJump flowSearchJump : searchByCategoryList) {
			String category = CommonSearchType
					.getShownameByValue(flowSearchJump.getCategoryCode());
			if (StringUtils.isNotEmpty(category)) {
				BarDataVo vo = new BarDataVo();
				vo.setAmount(flowSearchJump.getAmount());
				vo.setCategory(category);
				dataList.add(vo);
			}
		}
		JSONArray barData = JSONArray.fromObject(dataList);

		/* 折线图数据封装 */
		Map<String, Long> mapForLine = new HashMap<String, Long>();
		for (FlowSearchJump flowSearchJump : searchByDatelList) {
			String categoryName = CommonSearchType
					.getShownameByValue(flowSearchJump.getCategoryCode());
			String date = new SimpleDateFormat("yyyy/MM/dd")
					.format(flowSearchJump.getOperDate());
			String key = categoryName + "_" + date;
			long amount = 0;
			if (mapForLine.containsKey(key)) {
				amount = flowSearchJump.getAmount() + mapForLine.get(key);
			} else {
				amount = flowSearchJump.getAmount();
			}
			mapForLine.put(key, amount);
		}
		JSONArray lineData = parseJsonForLine(mapForLine);

		Map<String, Object> resultMap = new java.util.HashMap<>();
		resultMap.put("lineData", lineData);
		resultMap.put("barData", barData);
		return resultMap;
	}

	@RequestMapping("jumpByCategoryAjaxData")
	@ResponseBody
	public Map<String, Object> jumpByCategoryAjaxData(String flow_type,
			String choose_date, String platform_code, int station_id,
			String category_name, String key_type, int currentPage,
			int page_size) {
		Map<String, Object> resultMap = new java.util.HashMap<String, Object>();

		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(choose_date);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");

		// 获取平台相关code
		List<String> statisticCodeList = getPlatformCodeList(platform_code);
		RequestSearchParam param = new RequestSearchParam();
		param.setBeginDate(beginDate);
		param.setEndDate(endDate);
		param.setStatisticCodeList(statisticCodeList);
		param.setStation_id(station_id);
		param.setCategory_code(CommonSearchType
				.getValueByShowName(category_name));
		if ("pv".equals(flow_type)) {
			param.setJumpTableName("flow_search_jump_pv");
		} else {
			param.setJumpTableName("flow_search_jump_uv");
		}

		List<FlowSearchJump> searchList = new ArrayList<FlowSearchJump>();
		if ("切换标签".equals(key_type)) {
			searchList = flowSearchService.getSwitchByResult(param);
		} else {
			searchList = flowSearchService.getMoJumpByResult(param);
		}
		int successTotal = 0;
		int failTotal = 0;
		int switch_successTotal = 0;
		int switch_failTotal = 0;
		Map<String, Integer> keyAmountMap = new HashMap<>();
		for (FlowSearchJump flowSearchJump : searchList) {
			if ("切换标签".equals(key_type)) {
				switch_successTotal += flowSearchJump.getSwitchJumpSuccess();
				switch_failTotal += flowSearchJump.getJumpFailure();

				if (StringUtils.isNotBlank(flowSearchJump.getSwitchJumpJson())
						&& !"{}".equals(flowSearchJump.getSwitchJumpJson())) {
					JSONObject jsonObject = JSONObject
							.fromObject(flowSearchJump.getSwitchJumpJson());
					for (Iterator<?> iter = jsonObject.keys(); iter.hasNext();) {
						String key = (String) iter.next();
						Integer value = Integer.parseInt(jsonObject
								.getString(key));
						if (keyAmountMap.containsKey(key)) {
							value = keyAmountMap.get(key) + value;
						}
						keyAmountMap.put(key, value);
					}
				}
			} else {
				successTotal += flowSearchJump.getJumpSuccess();
				failTotal += flowSearchJump.getJumpFailure();
			}
		}
		if ("切换标签".equals(key_type)) {
			List<ProductInfoVO> list = new ArrayList<ProductInfoVO>();
			keyAmountMap = sortByValueDesc(keyAmountMap);
			for (Map.Entry<String, Integer> entry : keyAmountMap.entrySet()) {
				ProductInfoVO vo = new ProductInfoVO();
				vo.setProduct_name(entry.getKey());
				vo.setProduct_num(entry.getValue());
				list.add(vo);
			}
			PageInfo<ProductInfoVO> page = getListByPage(list, page_size,
					currentPage);
			resultMap.put("switch_successTotal", switch_successTotal);
			resultMap.put("switch_failTotal", switch_failTotal);
			resultMap.put("page", page);
			List<BarDataVo> dataList = new ArrayList<BarDataVo>();
			BarDataVo vo2 = new BarDataVo();
			vo2.setAmount(switch_failTotal);
			vo2.setCategory("跳转失败");
			dataList.add(vo2);
			BarDataVo vo = new BarDataVo();
			vo.setAmount(switch_successTotal);
			vo.setCategory("直接跳转成功");
			dataList.add(vo);
			JSONArray barData = JSONArray.fromObject(dataList);
			resultMap.put("barData", barData);
		} else {
			List<BarDataVo> dataList = new ArrayList<BarDataVo>();

			BarDataVo vo2 = new BarDataVo();
			vo2.setAmount(failTotal);
			vo2.setCategory("跳转失败");
			dataList.add(vo2);
			BarDataVo vo = new BarDataVo();
			vo.setAmount(successTotal);
			vo.setCategory("切换跳转成功");
			dataList.add(vo);
			JSONArray barData = JSONArray.fromObject(dataList);
			resultMap.put("barData", barData);
			resultMap.put("successTotal", successTotal);
			resultMap.put("failTotal", failTotal);
		}
		return resultMap;
	}

	/**
	 * @Title: resultAnalysis
	 * @Description: 搜索结果分析
	 * @param @param modelMap
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	@RequestMapping("resultAnalysis")
	public String resultAnalysis(ModelMap modelMap) {
		List<FlowSearchStation> stationList = flowSearchService.getStations();
		modelMap.addAttribute("stationList", stationList);
		return "application/searchStatistic/resultAnalysis";
	}

	@RequestMapping("resultAjaxData")
	@ResponseBody
	public Map<String, Object> resultAjaxData(String choose_date,
			String platform_code, int station_id) {

		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(choose_date);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");

		// 获取平台相关code
		List<String> statisticCodeList = getPlatformCodeList(platform_code);
		RequestSearchParam param = new RequestSearchParam();
		param.setBeginDate(beginDate);
		param.setEndDate(endDate);
		param.setStatisticCodeList(statisticCodeList);
		param.setStation_id(station_id);

		List<FlowSearchResult> searchByCategoryList = flowSearchService
				.getResultByCategory(param);
		/* 柱状图数据封装 */
		List<BarDataVo> dataList = new ArrayList<BarDataVo>();
		for (FlowSearchResult flowSearchResult : searchByCategoryList) {
			String category = CommonSearchType
					.getShownameByValue(flowSearchResult.getCategoryCode());
			if (StringUtils.isNotEmpty(category)) {
				BarDataVo vo = new BarDataVo();
				vo.setAmount(flowSearchResult.getAmountPV());
				vo.setCategory(category);
				dataList.add(vo);
			}
		}
		JSONArray barData = JSONArray.fromObject(dataList);

		/* 折线图数据封装 */
		List<FlowSearchResult> searchByDatelList = flowSearchService
				.getResultByDate(param);
		Map<String, Long> mapForLine = new HashMap<String, Long>();
		for (FlowSearchResult flowSearchResult : searchByDatelList) {
			String categoryName = CommonSearchType
					.getShownameByValue(flowSearchResult.getCategoryCode());
			String date = new SimpleDateFormat("yyyy/MM/dd")
					.format(flowSearchResult.getOperDate());
			String key = categoryName + "_" + date;
			long amount = 0;
			if (mapForLine.containsKey(key)) {
				amount = flowSearchResult.getAmountPV() + mapForLine.get(key);
			} else {
				amount = flowSearchResult.getAmountPV();
			}
			mapForLine.put(key, amount);
		}
		JSONArray lineData = parseJsonForLine(mapForLine);

		Map<String, Object> resultMap = new java.util.HashMap<>();
		resultMap.put("lineData", lineData);
		resultMap.put("barData", barData);
		return resultMap;
	}

	@RequestMapping("resultKeyAjaxData")
	@ResponseBody
	public Map<String, Object> resultKeyAjaxData(String choose_date,
			String platform_code, int station_id, String category_name,
			int currentPage, int page_size) {

		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(choose_date);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");

		// 获取平台相关code
		List<String> statisticCodeList = getPlatformCodeList(platform_code);
		RequestSearchParam param = new RequestSearchParam();
		param.setBeginDate(beginDate);
		param.setEndDate(endDate);
		param.setStatisticCodeList(statisticCodeList);
		param.setStation_id(station_id);
		param.setCategory_id(CommonSearchType.getValueByShowName(category_name));
		param.setPage_size(page_size);
		param.setPage_currentNum(currentPage);

		PageInfo<FlowSearchResult> page = flowSearchService
				.getResultByKey(param);

		Map<String, Object> resultMap = new java.util.HashMap<String, Object>();
		resultMap.put("page", page);
		return resultMap;
	}

	@RequestMapping("screeningAnalysis")
	public String screeningAnalysis(ModelMap modelMap) {
		CommonSearchType[] typeArr = CommonSearchType.getLabType();
		modelMap.addAttribute("typeArr", typeArr);
		return "application/searchStatistic/screeningAnalysis";
	}

	/**
	 * @Title: getVoList
	 * @Description: json串转成对象
	 * @param @param filterStr
	 * @param @param category_code
	 * @param @param platform_code
	 * @param @return 设定文件
	 * @return List<FilterConditionVO> 返回类型
	 * @throws
	 */
	private List<FilterConditionVO> getVoList(String filterStr,
			String category_code, String platform_code) {
		List<FilterConditionVO> listVO = new ArrayList<FilterConditionVO>();
		if (StringUtils.isNotBlank(filterStr)) {
			filterStr = filterStr.replaceAll("\r|\n", "");
			JSONObject jsonObject = JSONObject.fromObject(filterStr);
			for (Iterator<?> iter = jsonObject.keys(); iter.hasNext();) {
				FilterConditionVO vo = new FilterConditionVO();
				String key = (String) iter.next();
				String value = jsonObject.getString(key);
				String code = key.split("&")[0];// 传给接口的参数

				String condition1 = "";// 筛选的key值
				String codeValue = ""; // 筛选条件的value值的code
				String condition2 = "";// 筛选条件的value值的中文
				// App 796以后的版本
				if (code.contains("=")) {
					condition1 = code.split("=")[0];
					codeValue = code.split("=")[1];
					condition2 = key.split("&")[1];
					if (condition1.equals("hotelStar")) {
						condition2 = condition2.replaceAll("级", "");
					}
					FlowSearchFilterCondition param = new FlowSearchFilterCondition();
					param.setCategory_code(category_code);
					param.setCode(codeValue);
					param.setKey(condition1);
					if (flowSearchService.getFilterNameByCode(param) == null) {
						param.setName(condition2);
						flowSearchService.insertFilter(param);
					}
				} else {
					// APP 794,795版本的filter数据;h5的数据都走这边
					condition1 = code;
					if(key.split("&").length>1){
						codeValue = key.split("&")[1];
					}
					if ("price".equals(code) || "sort".equals(code)
							|| "hotelStar".equals(code)) {
						if (code.equals("sort")) {
							condition2 = CommonStringType
									.getSortNameByValue(codeValue);
							if (condition2.equals("0") || codeValue.equals("0")) {
								condition2 = "其他";
							}
							// 796之后版本sort传中文
							if (StringUtils.isEmpty(condition2)
									&& (!"今日可订".equals(codeValue))) {
								condition2 = codeValue;
							}
						} else if (code.equals("price")) {
							// 各平台794,795数据格式不一致，特殊处理
							if ("HOTEL".equals(category_code)) {
								condition2 = CommonStringType
										.getHotelPriceNameByValue(codeValue);
								if (StringUtils.isEmpty(condition2)) {
									if (codeValue.indexOf("150") > -1) {
										condition2 += "￥150以下,";
									}
									if (codeValue.indexOf("300") > -1) {
										condition2 += "￥151-￥300,";
									}
									if (codeValue.indexOf("450") > -1) {
										condition2 += "￥301-￥450,";
									}
									if (codeValue.indexOf("600") > -1) {
										condition2 = "￥451-￥600,";
									}
									if (codeValue.indexOf("1000") > -1) {
										condition2 += "￥601-￥1000";
									}
									if (codeValue.indexOf("1001") > -1) {
										condition2 += "￥1001以上";
									}
								}
								if (condition2.endsWith(",")) {
									condition2 = condition2.substring(0,
											condition2.length() - 1);
								}
							} else {
								condition2 = CommonStringType
										.getRoutePriceNameByValue(codeValue);
							}
						} else if (code.equals("hotelStar")) {
							condition2 = CommonStringType
									.getHotelStarNameByValue(codeValue)
									.replaceAll("级", "");
							if (StringUtils.isEmpty(condition2)) {
								if (codeValue.indexOf("100") > -1) {
									condition2 += "五星/豪华型,";
								}
								if (codeValue.indexOf("102") > -1) {
									condition2 += "四星/品质型,";
								}
								if (codeValue.indexOf("104") > -1) {
									condition2 += "三星/舒适型,";
								}
								if (codeValue.indexOf("106") > -1) {
									condition2 = "二星/简约型,";
								}
								if (codeValue.indexOf("109") > -1) {
									condition2 += "其他/其他";
								}
							}
							if (condition2.endsWith(",")) {
								condition2 = condition2.substring(0,
										condition2.length() - 1);
							}
						}
					} else {
						// 之前版本 多选情况
						if ("routeNum".equals(condition1)) {
							String strArr[] = codeValue.split(",");
							for (String str : strArr) {
								FlowSearchFilterCondition param = new FlowSearchFilterCondition();
								param.setCategory_code(category_code);
								param.setCode(str);
								param.setKey(code);
								FlowSearchFilterCondition filterCondition = flowSearchService
										.getFilterNameByCode(param);
								if (filterCondition != null) {
									condition2 += filterCondition.getName()
											+ ",";
								} else {
									condition2 = codeValue + ",";
								}
							}
							condition2 = condition2.substring(0,
									condition2.length() - 1);
						} else {
							FlowSearchFilterCondition param = new FlowSearchFilterCondition();
							param.setCategory_code(category_code);
							param.setCode(codeValue);
							param.setKey(code);
							FlowSearchFilterCondition filterCondition = flowSearchService
									.getFilterNameByCode(param);
							if (filterCondition != null) {
								condition2 = filterCondition.getName();
							} else {
								condition2 = codeValue;
							}
						}

					}
				}
				String conditionStr = CommonSearchFilter.getShowName(
						condition1, category_code);
				// 判断此筛选条件是否有父级，有父级则会有三层统计数据，没有父级则有两级，但是今日可订特殊只需一级统计
				if (StringUtils.isNotEmpty(conditionStr)
						&& StringUtils.isNotEmpty(condition2)) {
					if (CommonSearchFilter.hasParentFlag(condition1,
							category_code)) {
						List<SecondConditionVO> children = new ArrayList<SecondConditionVO>();
						String parentConditionStr = CommonSearchFilter
								.getParentShowName(condition1, category_code);
						// 二级对象
						SecondConditionVO secondVO = new SecondConditionVO();
						secondVO.setCondition(conditionStr);
						secondVO.setAmount(Long.parseLong(value));
						if (!(conditionStr.indexOf("景点活动") > -1 || conditionStr
								.indexOf("促销活动") > -1)) {
							List<ChildConditionVO> thirdChildren = new ArrayList<ChildConditionVO>();
							// 三级 若出现多选情况 ，用逗号分开
							String[] conditionArr = condition2.split(",");
							for (String condition : conditionArr) {
								ChildConditionVO childVO = new ChildConditionVO();
								childVO.setCondition(condition);
								childVO.setAmount(Long.parseLong(value));
								thirdChildren.add(childVO);
							}
							secondVO.setSechildren(thirdChildren);
						}
						children.add(secondVO);
						vo.setCondition(parentConditionStr);
						vo.setAmount(Integer.parseInt(value));
						vo.setChildren(children);
					} else {
						if (!"todayTicketFlag".equals(condition1)) {
							List<SecondConditionVO> children = new ArrayList<SecondConditionVO>();
							SecondConditionVO secondVO = new SecondConditionVO();
							secondVO.setCondition(condition2);
							secondVO.setAmount(Long.parseLong(value));
							children.add(secondVO);
							vo.setChildren(children);
						}
						vo.setCondition(conditionStr);
						vo.setAmount(Integer.parseInt(value));
					}

					listVO.add(vo);
				}
			}
		}
		return listVO;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("filterAjaxData")
	@ResponseBody
	public Map<String, Object> filterAjaxData(String choose_date,
			String platform_code, String category_id, HttpSession httpSession) {
		Map<String, Object> resultMap = new java.util.HashMap<String, Object>();

		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(choose_date);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");

		// 获取平台相关code
		List<String> statisticCodeList = getPlatformCodeList(platform_code);
		RequestSearchParam param = new RequestSearchParam();
		param.setBeginDate(beginDate);
		param.setEndDate(endDate);
		param.setStatisticCodeList(statisticCodeList);
		param.setCategory_id(category_id);

		String[] dateAxis = DateUtil.getBetweenTime(beginDate, endDate,
				"yyyy/MM/dd");

		// json串 按照日期 获取搜索条件对象list 注：<日期,搜索条件list>
		Map<String, List<FilterConditionVO>> dataFilterMap = new HashMap<String, List<FilterConditionVO>>();

		String key = choose_date + platform_code + category_id
				+ "dataFilterMap";
		if (httpSession.getAttribute(key) != null) {
			dataFilterMap = (Map<String, List<FilterConditionVO>>) httpSession
					.getAttribute(key);
		} else {
			List<FlowSearchFilter> filterList = flowSearchService
					.getFilterList(param);
			for (FlowSearchFilter flowSearchFilter : filterList) {
				String filterCondition = flowSearchFilter.getFilterCondition()
						.trim();
				List<FilterConditionVO> listVO = getVoList(filterCondition,
						category_id, platform_code);

				if (dataFilterMap.containsKey(DateUtil.formatDate(
						flowSearchFilter.getOperDate(), "yyyy/MM/dd"))) {
					listVO.addAll(dataFilterMap.get(DateUtil.formatDate(
							flowSearchFilter.getOperDate(), "yyyy/MM/dd")));
				}
				dataFilterMap.put(DateUtil.formatDate(
						flowSearchFilter.getOperDate(), "yyyy/MM/dd"), listVO);
			}
			httpSession.setAttribute(key, dataFilterMap);
		}

		// <搜索条件,时间段内逐日的搜素量数组>
		Map<String, Long> amountLineMap = new HashMap<String, Long>();
		Map<String, Long> amountBarMap = new HashMap<String, Long>();
		if (!dataFilterMap.isEmpty()) {
			for (int i = 0; i < dateAxis.length; i++) {
				if (dataFilterMap.containsKey(dateAxis[i])) {
					List<FilterConditionVO> list = dataFilterMap
							.get(dateAxis[i]);
					for (FilterConditionVO filterConditionVO : list) {
						// 按照 搜索条件 统计出当前搜索条件每日的搜索系数
						int amount = 0;
						if (filterConditionVO.getChildren() == null
								|| filterConditionVO.getChildren().size() == 0) {
							amount = filterConditionVO.getAmount();
						} else {
							List<SecondConditionVO> voList = filterConditionVO
									.getChildren();
							int childAmount = 0;
							for (SecondConditionVO vo : voList) {
								if (vo.getSechildren() != null
										&& vo.getSechildren().size() != 0) {
									for (ChildConditionVO child : vo
											.getSechildren()) {
										childAmount += child.getAmount();
									}
								} else {
									childAmount += vo.getAmount();
								}
							}
							amount = childAmount;
						}
						long filterAmount = 0l;
						long filterBarAmount = 0l;
						if (StringUtils.isNotEmpty(filterConditionVO
								.getCondition())) {
							// 折线图数据封装---搜索条件,时间 相同 map的数量加上当前的数量
							if (amountLineMap.containsKey(filterConditionVO
									.getCondition() + "_" + dateAxis[i])) {
								filterAmount = amountLineMap
										.get(filterConditionVO.getCondition()
												+ "_" + dateAxis[i]);
							}
							filterAmount += amount;
							amountLineMap.put(filterConditionVO.getCondition()
									+ "_" + dateAxis[i], filterAmount);

							// 柱状图数据封装--- 搜索条件 相同 map的数量加上当前的数量
							if (amountBarMap.containsKey(filterConditionVO
									.getCondition())) {
								filterBarAmount = amountBarMap
										.get(filterConditionVO.getCondition());
							}
							filterBarAmount += amount;
							amountBarMap.put(filterConditionVO.getCondition(),
									filterBarAmount);
						}
					}
				}
			}
		}
		JSONArray lineData = parseJsonForLine(amountLineMap);
		JSONArray barData = parseJsonForBar(amountBarMap);
		resultMap.put("lineData", lineData);
		resultMap.put("barData", barData);
		return resultMap;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("childrenAjaxData")
	@ResponseBody
	public Map<String, Object> childrenAjaxData(String choose_date,
			String platform_code, String category_id, String type,
			HttpSession httpSession) {
		Map<String, Object> resultMap = new java.util.HashMap<String, Object>();
		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(choose_date);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");
		// 获取平台相关code
		List<String> statisticCodeList = getPlatformCodeList(platform_code);
		RequestSearchParam param = new RequestSearchParam();
		param.setBeginDate(beginDate);
		param.setEndDate(endDate);
		param.setStatisticCodeList(statisticCodeList);
		param.setCategory_id(category_id);
		List<FilterConditionVO> list = new ArrayList<FilterConditionVO>();

		String key = choose_date + platform_code + category_id
				+ "dataFilterList";
		if (httpSession.getAttribute(key) != null) {
			list = (List<FilterConditionVO>) httpSession.getAttribute(key);
		} else {
			List<FlowSearchFilter> filterList = flowSearchService
					.getFilterList(param);
			for (FlowSearchFilter flowSearchFilter : filterList) {
				String filterCondition = flowSearchFilter.getFilterCondition()
						.trim();
				list.addAll(getVoList(filterCondition, category_id,
						platform_code));
			}
			httpSession.setAttribute(key, list);
		}

		int code = 1;
		List<String> seConditionList = new ArrayList<String>();
		List<Long> amountList = new ArrayList<Long>();
		Map<String, Long> childMap = new HashMap<String, Long>();
		Map<String, Object> endMap = new HashMap<String, Object>();

		Map<String, Object> sortTotalMap = new HashMap<String, Object>();
		Map<String, Long> sortSecondMap = new HashMap<String, Long>();

		for (FilterConditionVO filterConditionVO : list) {
			if (type.equals(filterConditionVO.getCondition())) {
				if (filterConditionVO.getChildren() != null
						&& filterConditionVO.getChildren().size() != 0) {
					List<SecondConditionVO> children = filterConditionVO
							.getChildren();
					for (SecondConditionVO child : children) {
						if (child.getSechildren() != null
								&& child.getSechildren().size() != 0) {
							code = 3;// 三级
							Map<String, Long> sortMap = new HashMap<String, Long>();
							Map<String, Long> thirdMap = new HashMap<String, Long>();
							long totalAmount = 0l;
							if (endMap.containsKey(child.getCondition())) {
								sortMap = (Map<String, Long>) sortTotalMap
										.get(child.getCondition());
								totalAmount = childMap.get(child.getCondition());
							}
							for (ChildConditionVO vo : child.getSechildren()) {
								totalAmount += vo.getAmount();
								long count = vo.getAmount();
								if (sortMap.containsKey(vo.getCondition())) {
									count += sortMap.get(vo.getCondition());
								}
								sortMap.put(vo.getCondition(), count);
							}
							sortMap = sortByValueDesc(sortMap);
							// 最终子集排序取前十
							int index = 0;
							for (Map.Entry<String, Long> entry : sortMap
									.entrySet()) {
								if (index < 10) {
									thirdMap.put(entry.getKey(),
											entry.getValue());
									index++;
								} else {
									break;
								}
							}
							JSONArray jsonArr = parseJsonForBar(thirdMap);
							endMap.put(child.getCondition(), jsonArr);
							childMap.put(child.getCondition(), totalAmount);
							sortTotalMap.put(child.getCondition(), sortMap);
						} else {
							code = 2;// 两级
							if (sortSecondMap.containsKey(child.getCondition())) {
								int index = seConditionList.indexOf(child
										.getCondition());
								long amount = amountList.get(index);
								amountList.set(index,
										amount + child.getAmount());
								sortSecondMap.put(child.getCondition(),
										sortSecondMap.get(child.getCondition())
												+ child.getAmount());
							} else {
								seConditionList.add(child.getCondition());
								amountList.add(child.getAmount());
								sortSecondMap.put(child.getCondition(),
										child.getAmount());
							}
						}
					}
				} else {
					code = 1;// 一级
				}
			}
		}
		if (code == 3) {
			resultMap.put("childMap", childMap);
			resultMap.put("endMap", endMap);
		} else if (code == 2) {
			sortSecondMap = sortByValueDesc(sortSecondMap);
			List<BarDataVo> barList = new ArrayList<>();
			// 最终子集排序取前十
			int index = 0;
			for (Map.Entry<String, Long> entry : sortSecondMap.entrySet()) {
				if (index < 10) {
					if (index == 0) {
						barList.clear();
					}
					BarDataVo barDataVo = new BarDataVo();
					barDataVo.setCategory(entry.getKey());
					barDataVo.setAmount(entry.getValue());
					barList.add(barDataVo);
					index++;
				} else {
					break;
				}
			}
			JSONArray sechildData = JSONArray.fromObject(barList);
			resultMap.put("sechildData", sechildData);
		}
		resultMap.put("code", code);
		return resultMap;
	}

	/**
	 * @Title: pageSkipAnalysis
	 * @Description: 翻页分析
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	@RequestMapping("pageSkipAnalysis")
	public String pageSkipAnalysis() {
		return "application/searchStatistic/pageSkipAnalysis";
	}

	@RequestMapping("pageSkipAjaxData")
	@ResponseBody
	public Map<String, Object> pageSkipAjaxData(String choose_date,
			String platform_code) {
		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(choose_date);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");
		// 获取平台相关code
		List<String> statisticCodeList = getPlatformCodeList(platform_code);
		RequestSearchParam param = new RequestSearchParam();
		param.setBeginDate(beginDate);
		param.setEndDate(endDate);
		param.setStatisticCodeList(statisticCodeList);
		List<FlowSearchPaging> pagingList = flowSearchService
				.getPagingByCategory(param);

		List<BarDataVo> dataList = new ArrayList<BarDataVo>();
		for (FlowSearchPaging searchPaging : pagingList) {
			String category = CommonSearchType.getShownameByValue(searchPaging
					.getCategoryCode());
			if (StringUtils.isNotEmpty(category)) {
				BarDataVo vo = new BarDataVo();
				vo.setAmount(searchPaging.getAmount());
				vo.setCategory(category);
				dataList.add(vo);
			}
		}
		JSONArray barData = JSONArray.fromObject(dataList);
		Map<String, Object> resultMap = new java.util.HashMap<String, Object>();
		resultMap.put("barData", barData);
		return resultMap;
	}

	@RequestMapping("pageCodeAjaxData")
	@ResponseBody
	public Map<String, Object> pageCodeAjaxData(String choose_date,
			String platform_code, String category_name) {
		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(choose_date);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");
		// 获取平台相关code
		List<String> statisticCodeList = getPlatformCodeList(platform_code);
		RequestSearchParam param = new RequestSearchParam();
		param.setBeginDate(beginDate);
		param.setEndDate(endDate);
		param.setStatisticCodeList(statisticCodeList);
		param.setCategory_id(CommonSearchType.getValueByShowName(category_name));

		List<FlowSearchPaging> pagingList = flowSearchService
				.getPagingByPageCode(param);
		int pageNum = 0;
		List<BarDataVo> dataList = new ArrayList<BarDataVo>();
		for (FlowSearchPaging searchPaging : pagingList) {
			if (pageNum < 10) {
				BarDataVo vo = new BarDataVo();
				vo.setAmount(searchPaging.getAmount());
				vo.setCategory("页码" + searchPaging.getPageNum());
				dataList.add(vo);
			}
			pageNum++;
		}
		JSONArray pageData = JSONArray.fromObject(dataList);

		Map<String, Object> resultMap = new java.util.HashMap<String, Object>();
		resultMap.put("pageData", pageData);
		resultMap.put("pageNum", pageNum);
		return resultMap;
	}

	/**
	 * @Title: getPlatformCodeList
	 * @Description: 获取平台相关code
	 * @param @param platform_code
	 * @param @return 设定文件
	 * @return List<String> 返回类型
	 * @throws
	 */
	private List<String> getPlatformCodeList(String platform_code) {
		List<String> statisticCodeList = new ArrayList<String>();
		// app
		if ("app".equals(platform_code)) {
			statisticCodeList = statisticCodeService.getDeviceCodeApp();
		}
		// wap
		else if ("wap".equals(platform_code)) {
			statisticCodeList = statisticCodeService
					.getDeviceCodeByLikeName("wap");
		}
		// pc
		else if ("pc".equals(platform_code)) {
			statisticCodeList = statisticCodeService
					.getDeviceCodeByLikeName("pc");
		} else {
			statisticCodeList = null;
		}
		return statisticCodeList;
	}

	/**
	 * @Title: getQueryDate
	 * @Description: 查询条件起始时间的处理
	 * @param @param choose_date
	 * @param @return 设定文件
	 * @return Map<String,Date> 返回类型
	 * @throws
	 */
	private Map<String, Date> getQueryDate(String choose_date) {
		Map<String, Date> map = new HashMap<>();
		DateTimeFormatter formater = DateTimeFormat
				.forPattern("MM/dd/yyyy HH:mm:ss");
		Date beginDate = DateTime.parse(
				choose_date.split("-")[0].trim() + " 00:00:00", formater)
				.toDate();
		Date endDate = DateTime.parse(
				choose_date.split("-")[1].trim() + " 23:59:59", formater)
				.toDate();
		map.put("beginDate", beginDate);
		map.put("endDate", endDate);
		return map;
	}

	/**
	 * @Title: getListByPage
	 * @Description: list分页显示
	 * @param @param list
	 * @param @param page_size
	 * @param @param current_page
	 * @param @return 设定文件
	 * @return PageInfo<T> 返回类型
	 * @throws
	 */
	private PageInfo<ProductInfoVO> getListByPage(List<ProductInfoVO> list,
			int page_size, int current_page) {
		if (null == list) {
			return null;
		}
		PageInfo<ProductInfoVO> page = new PageInfo<>();
		int totalNum = list.size();
		int pages = totalNum % page_size == 0 ? totalNum / page_size : totalNum
				/ page_size + 1;
		int pageEndRow = 0;
		int pageStartRow = 0;
		if (current_page * page_size < totalNum) {// 判断是否为最后一页 
			pageEndRow = current_page * page_size;
		} else {
			pageEndRow = totalNum;
		}
		pageStartRow = page_size * (current_page - 1) + 1;
		page.setStartRow(pageStartRow);
		page.setEndRow(pageEndRow);
		page.setPageSize(page_size);
		page.setIsFirstPage(current_page == 1);
		page.setIsLastPage(current_page == pages);
		page.setHasNextPage(current_page < pages);
		page.setHasPreviousPage(current_page > 1);
		page.setPages(pages);
		page.setPageNum(current_page);
		page.setTotal(totalNum);

		List<ProductInfoVO> resultList = null;
		if (list.size() > 0) {
			resultList = list.subList(pageStartRow - 1, pageEndRow);
		}
		page.setList(resultList);
		return page;
	}

	/**
	 * @Title: parseJsonForBar
	 * @Description: 柱状图数据：map封装对象转json数组
	 * @param @param mapForBar
	 * @param @return 设定文件
	 * @return JSONArray 返回类型
	 * @throws
	 */
	private JSONArray parseJsonForBar(Map<String, Long> mapForBar) {
		List<BarDataVo> barList = new ArrayList<>();
		for (Map.Entry<String, Long> entry : mapForBar.entrySet()) {
			BarDataVo barDataVo = new BarDataVo();
			barDataVo.setCategory(entry.getKey());
			barDataVo.setAmount(entry.getValue());
			barList.add(barDataVo);
		}
		JSONArray barData = JSONArray.fromObject(barList);
		return barData;
	}

	/**
	 * @Title: parseJsonForLine
	 * @Description: 折线图数据：map封装对象转json数组
	 * @param @param mapForLine
	 * @param @return 设定文件
	 * @return JSONArray 返回类型
	 * @throws
	 */
	private JSONArray parseJsonForLine(Map<String, Long> mapForLine) {
		List<LineDataVo> lineList = new ArrayList<>();
		for (Map.Entry<String, Long> entry : mapForLine.entrySet()) {
			LineDataVo lineDataVo = new LineDataVo();
			lineDataVo.setTime(entry.getKey().split("_")[1]);
			lineDataVo.setCategory(entry.getKey().split("_")[0]);
			lineDataVo.setAmount(entry.getValue());
			lineList.add(lineDataVo);
		}
		JSONArray lineData = JSONArray.fromObject(lineList);
		return lineData;
	}
}
